package com.atguigu.orderservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.mapper.TOrderMapper;
import com.atguigu.orderservice.service.TOrderService;
import com.atguigu.orderservice.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-09-29
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String saveOrder(String courseId, String memberId) {

            //远程调用课程服务，根据课程id获取课程信息
            Map<String,String> courseDto = eduClient.getCourseInfoById(courseId);

            //远程调用用户服务，根据用户id获取用户信息
            Map<String,String> ucenterMember = ucenterClient.getInfoUc(memberId);

            //创建订单
            TOrder order = new TOrder();
            order.setOrderNo(OrderNoUtil.getOrderNo());
            order.setCourseId(courseId);
            if(courseDto.get("title")!=null && courseDto.get("title")!=""){
                order.setCourseTitle(courseDto.get("title"));
            }else{
                order.setCourseTitle("");
            }
            if(courseDto.get("cover")!=null && courseDto.get("cover") !=""){
                order.setCourseCover(courseDto.get("cover"));
            }else{
                order.setCourseCover("");
            }
            if(courseDto.get("teacherName") != null && courseDto.get("teacherName") !=""){
                order.setTeacherName(courseDto.get("teacherName"));
            }else{
                order.setTeacherName("");
            }
            if(courseDto.get("price")!=null && courseDto.get("price") !=""){
                BigDecimal price = new BigDecimal(courseDto.get("price"));
                order.setTotalFee(price);
            }else{
                BigDecimal price = new BigDecimal(courseDto.get("0"));
                order.setTotalFee(price);
            }
            order.setMemberId(memberId);
            if(courseDto.get("mobile")!=null && courseDto.get("mobile") !=""){
                order.setMobile(ucenterMember.get("mobile"));
            }else{
                order.setMobile("");
            }
            if(courseDto.get("nickname") != null && courseDto.get("nickname")!=""){
                order.setNickname(ucenterMember.get("nickname"));
            }else{
                order.setNickname("");
            }
            order.setStatus(0);
            order.setPayType(1);
            order.setGmtCreate(new Date());
            order.setGmtModified(new Date());
            baseMapper.insert(order);

            return order.getOrderNo();




    }
}
